﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Widgets.MindMap
{
    public class TitleFriendAdorner : Adorner
    {
        /// <summary>
        /// 用以表示标题有链接标题的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入标题的“MainBorder”主子元素。</param>
        public TitleFriendAdorner(UIElement adornedElement, MindMapTitle masterTitle)
            : base(adornedElement)
        {
            this.masterTitle = masterTitle;

            //this.Opacity = 0.75;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            //this.SnapsToDevicePixels = true;
            //this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

        }

        private MindMapTitle masterTitle;

        public MindMapTitle MasterTitle
        {
            get { return masterTitle; }
        }

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout system as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (this.Visibility != System.Windows.Visibility.Visible || masterTitle == null || masterTitle.MasterView == null) return;

            Brush brush = Brushes.White;
            Pen pen;

            double defaultFontSize = MasterTitle.MasterView.TitleDefaultFontSize;

            if (double.IsNaN(defaultFontSize))
            {
                defaultFontSize = (double)Application.Current.TryFindResource("title_default_font_size");
            }

            pen = new Pen(brush, Math.Max(defaultFontSize / 4, 4));

            Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize);

            //adornedElementRect.X -= 6; adornedElementRect.Y -= 6;
            //adornedElementRect.Width += 12; adornedElementRect.Height += 12;

            //double halfHeight = adornedElementRect.Height / 2;
            //drawingContext.DrawRoundedRectangle(null, pen, adornedElementRect, halfHeight, halfHeight);

            drawingContext.DrawRectangle(brush, null, new Rect(adornedElementRect.Right - defaultFontSize, adornedElementRect.Top - defaultFontSize / 2, defaultFontSize, defaultFontSize));
        }
    }
}
